package com.leha.mp.core.services.imp;

import com.leha.mp.core.dao.PlUserMapper;
import com.leha.mp.core.entity.mybatis.PlUser;
import com.leha.mp.core.services.PushServices;
import com.leha.mp.core.services.WxMPServices;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.util.HashMap;

@Log4j2
@Service
public class WebSocketPushServicesImpl implements PushServices {
    private HashMap<Integer, WebSocketSession> userMap = new HashMap<>();
    private HashMap<Integer, WebSocketSession> doctorMap = new HashMap<>();
    private HashMap<String, Integer> sidToidMap = new HashMap<>();
    @Autowired
    WxMPServices mpServices;
    @Autowired
    PlUserMapper userMapper;

    @Override
    public Boolean UserEnter(Integer uid, WebSocketSession session) {
        userMap.put(uid, session);
        sidToidMap.put(session.getId(), uid);
        return true;
    }

    @Override
    public Boolean DoctorEnter(Integer did, WebSocketSession session) {
        doctorMap.put(did, session);
        sidToidMap.put(session.getId(), -did);
        return true;
    }

    @Override
    public Boolean userDisconnect(String sessionId) {
        Integer key = sidToidMap.get(sessionId);
        if(key>0) {
            UserLeave(key);
        } else {
            DoctorLeave(-key);
        }
        return null;
    }

    @Override
    public Boolean UserLeave(Integer uid) {
        userMap.remove(uid);
        return true;
    }

    @Override
    public Boolean DoctorLeave(Integer did) {
        doctorMap.remove(did);
        return true;
    }

    @Override
    public Boolean SendMessageToDoctor(Integer did, String msgContent) {
        WebSocketSession session = doctorMap.get(did);
        if(session == null) {
            log.info("Doctor not online, Failed to send Message");
            return false;
        }
        try {
            session.sendMessage(new TextMessage(msgContent));
            return true;
        } catch (IOException e) {
            log.error("SendMsgToDoctor:"+did.toString()+"Failed,"+e.getMessage());
            return false;
        }

    }

    @Override
    public Boolean SendMessageToUser(Integer uid, String msgContent) {
        WebSocketSession session = userMap.get(uid);
        if(session == null) {
            log.info("User not online, Failed to send Message");
            return false;
        }
        try {
            session.sendMessage(new TextMessage(msgContent));
            return true;
        } catch (IOException e) {
            log.error("SendMsgToUser:"+uid.toString()+"Failed,"+e.getMessage());
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Boolean SendMessageToUserWithMp(String unionid, String json) {
        return mpServices.sendMpMsg(unionid, json);
    }

    @Override
    public Boolean SendMessageToUserWithMp(Integer userId, String json) {
        PlUser user = userMapper.selectByPrimaryKey(userId);
        return SendMessageToUserWithMp(user.getUnionid(), json);
    }
}
